
//
//  ZMChangeCityViewController.m
//  DaMei
//
//  Created by mac on 16/2/23.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ZMChangeCityViewController.h"
#import "ZMcityGroupsModel.h"
#import "ZMCityseachTableViewController.h"

@interface ZMChangeCityViewController () <UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UITableView *citesTableView;

@property (nonatomic, strong) NSArray *citesDate;

@property (nonatomic, weak) ZMCityseachTableViewController *citySeachVC;

@end

@implementation ZMChangeCityViewController


///懒加载

- (ZMCityseachTableViewController *)citySeachVC {

    if (_citySeachVC == nil) {
        
        ZMCityseachTableViewController *citySeachVC = [ZMCityseachTableViewController new];
        
        [self addChildViewController:citySeachVC];
        
        [self.view addSubview:citySeachVC.view];
        
        [citySeachVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.citesTableView);
        }];
        
    
        _citySeachVC = citySeachVC;
    }
    
    return _citySeachVC;
}

-(NSArray *)citesDate {

    if (_citesDate == nil) {
        
        _citesDate = [ZMcityGroupsModel objectArrayWithFilename:@"cityGroups.plist"];
    }
    return _citesDate;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"切换城市";
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImageName:@"btn_navigation_close" hightlightImageName:@"btn_navigation_close_hl" target:self action:@selector(backClick)];
    
    self.citesTableView.sectionIndexColor = [UIColor colorWithRed:21/255.0 green:188/255.0 blue:173/255.0 alpha:1];
//    self.citesTableView.tintColor = [UIColor greenColor];
    
    self.searchBar.tintColor = [UIColor colorWithRed:21/255.0 green:188/255.0 blue:173/255.0 alpha:1];
}

- (void)backClick {

    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark 数据源方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.citesDate.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    ZMcityGroupsModel *cityGroupsModel = self.citesDate[section];
    
    return cityGroupsModel.cities.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *ID = @"citesTableViewCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    ZMcityGroupsModel *cityGroupsModel = self.citesDate[indexPath.section];
    
    cell.textLabel.text = cityGroupsModel.cities[indexPath.row];
    
    return cell;
}


#pragma mark tableView代理方法

//设置组标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    ZMcityGroupsModel *cityModel = self.citesDate[section];
    return cityModel.title;
}
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
//
//    NSMutableArray *tempArray = [NSMutableArray array];
//    
//    for (ZMcityGroupsModel *cityGroupModel in self.citesDate) {
//        
//        [tempArray addObject:cityGroupModel.title];
//    }
//    return tempArray;
    
    
    //KVC 遍历属性赋值
    return [self.citesDate valueForKey:@"title"];
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //获取选中城市的名字  根据section获取分组的数据
    ZMcityGroupsModel *cityGroupModel = self.citesDate[indexPath.section];
    //发送通知  城市名
    [ZMNotificationCenter postNotificationName:ZMCityDidChangeNotifacation object:nil userInfo:@{ZMSelectCityName : cityGroupModel.cities[indexPath.row]}];
    // 移除视图
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark SeachBar代理方法

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {

    //1.隐藏导航栏
    self.navigationController.navigationBarHidden = YES;
    
    //2.改变搜索框背景
    self.searchBar.backgroundImage = [UIImage imageNamed:@"bg_login_textfield_hl"];
    
    //3.搜索框出现取消按钮
    [self.searchBar setShowsCancelButton:YES animated:YES];
    
      //3.1修改去取消按钮为中文，遍历私有类
    for (UIView *subView in self.searchBar.subviews.firstObject.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            [(UIButton *)subView setTitle:@"取消" forState:UIControlStateNormal];
        }
    }
    
    //4.添加遮盖按钮
    UIButton *coverButton = [UIButton buttonWithType:UIButtonTypeCustom];
    coverButton.backgroundColor = [UIColor blackColor];
    coverButton.alpha = 0.5;
    coverButton.frame = self.citesTableView.frame;
    
    coverButton.tag = 110;
    
    [coverButton addTarget:self action:@selector(coverButtonClick) forControlEvents:UIControlEventTouchUpInside ];
    [self.view insertSubview:coverButton belowSubview:self.citySeachVC.view];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {

    self.navigationController.navigationBarHidden = NO;
    self.searchBar.backgroundImage = [UIImage imageNamed:@"bg_login_textfield"];
    [self.searchBar setShowsCancelButton:NO animated:YES];
    UIButton * coverBtn = [self.view viewWithTag:110];
    [coverBtn removeFromSuperview];
    
    // 取消搜索结果控制器的显示
    self.citySeachVC.view.hidden = YES;
    
    // 清空搜索框的文字
    searchBar.text = @"";
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

    [searchBar resignFirstResponder];
}

- (void)coverButtonClick {

    [self.searchBar resignFirstResponder];
}

#pragma mark 搜索框文字发生改变的时候调用
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

    if (searchText.length > 0) {
        
        self.citySeachVC.view.hidden = NO;
        
        self.citySeachVC.searchText = searchText;
        
    }else {
    
        self.citySeachVC.view.hidden = YES;
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
